﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.Logic.POI;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;
using Action = TreeSharp.Action;
using Sequence = TreeSharp.Sequence;
using Styx.WoWInternals;


namespace Healz.Druid.Spells
{
    public static class TreeofLife
    {
        public const string SpellName = "Tree of Life";

        public static bool Pulse()
        {
            // Our Checks
            if (StyxWoW.Me.ManaPercent < 65) return false;
            if (!SpellManager.Spells[SpellName].CanCast) return false;
            if (PlayersUnderFlag() <= Main.cSettings.TreeOfLife_People) return false;
            if (SpellManager.Spells[SpellName].Cooldown) return false;

            // Cast and Write
            Utils.WriteCast(SpellName);
            SpellManager.Cast(SpellName, StyxWoW.Me);

            return true;
        }

        // Gets people under our flag to trigger Tree of life
        public static int PlayersUnderFlag()
        {
            return (int)(from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(true,true)
                         where !Unit.Dead
                         where Unit.Distance2D < 40
                         where Unit.CurrentHealth > 1
                         where Unit.HealthPercent < Main.cSettings.TreeOfLife_Percent
                         where Unit.IsFriendly
                         select Unit).Count();
        }
    }
}
